# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import mysql.connector

class TutorialPipeline:
    def open_spider(self,spider):
        print('pipeline start ********************************\n'*3)
        self.conn=mysql.connector.connect(user='root',password='12345600',database='qingmeng')
        self.cursor=self.conn.cursor()
        
        
    def process_item(self, item, spider):
        adapter = ItemAdapter(item)
        if adapter:
            self.cursor.execute('insert into poem (title,author,dynsty,url,content)\
                values (%s,%s,%s,%s,%s)',[adapter.get('title'),adapter.get('author'),\
                    adapter.get('dynsty'),\
                    adapter.get('url'),\
                    adapter.get('content')]\
                    )
            self.conn.commit()
        return item
    def close_spider(self,spider):
        self.cursor.close()
        self.conn.close()

